package com.yudi.blog.bussiness.controller.admin;

import com.yudi.blog.bussiness.dto.Blog;
import com.yudi.blog.bussiness.dto.User;
import com.yudi.blog.bussiness.service.BlogService;
import com.yudi.blog.bussiness.service.TypeService;
import com.yudi.blog.bussiness.service.TagService;
import com.yudi.blog.bussiness.vo.BlogQueryVO;
import com.yudi.blog.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * @Author YuDi
 * @Date 2021/11/10 15:36
 * @Version 1.0
 */
@Controller
@RequestMapping("/admin")
public class BlogController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;


    @GetMapping("/blogs")
    public String list(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                       BlogQueryVO blog, Model model){
        model.addAttribute("types", typeService.listType());
        model.addAttribute("page", blogService.listBlog(pageable, blog));
        return "admin/blogs";
    }


    @PostMapping("/blogs/search")
    public String search(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                         BlogQueryVO blog,
                         Model model){
        model.addAttribute("page", blogService.listBlog(pageable, blog));
        return "admin/blogs :: blogList";
    }

    @PostMapping("/blogs")
    public String add(Blog blog, RedirectAttributes redirectAttributes, HttpSession session){
        blog.setUser((User)session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getTagIds()));
        blog.setCreateTime(new Date());
        blog.setViews(0);
        Blog t = blogService.save(blog);
        if(t == null){
            redirectAttributes.addFlashAttribute("message", "博客添加失败");
        } else {
            redirectAttributes.addFlashAttribute("message", "博客添加成功");
        }
        return "redirect:/admin/blogs";
    }



    @GetMapping("/blogs/input")
    public String input(Model model){
        model.addAttribute("tags", tagService.listTag());
        model.addAttribute("types", typeService.listType());
        model.addAttribute("blog", new Blog());
        return "admin/blogs-input";
    }


    @GetMapping("/blogs/input/{id}")
    public String editInput(@PathVariable Long id, Model model){

        model.addAttribute("tags", tagService.listTag());
        model.addAttribute("types", typeService.listType());

        Blog t = blogService.getBlog(id);
        t.init();
        model.addAttribute("blog", t);
        return "admin/blogs-input";
    }


    @GetMapping("/blogs/delete/{id}")
    public String delete(@PathVariable Long id, RedirectAttributes redirectAttributes){
        blogService.delete(id);
        redirectAttributes.addFlashAttribute("message", "博客删除成功");
        return "redirect:/admin/blogs";
    }


    @PostMapping("/blogs/input/{id}")
    public String update(@PathVariable Long id, Blog blog, RedirectAttributes redirectAttributes){
        Boolean flag = StringUtils.isEmpty(blog.getTitle()) ||
                StringUtils.isEmpty(blog.getContent()) ||
                blog.getType().getId() == null ||
                StringUtils.isEmpty(blog.getFirstPicture()) ||
                StringUtils.isEmpty(blog.getTitle()) ||
                StringUtils.isEmpty(blog.getDescription());

        if (flag) {
            redirectAttributes.addFlashAttribute("message", "博客更新失败");
            return "redirect:/admin/blogs";
        }
        Blog t = blogService.update(id, blog);
        if(t == null){
            redirectAttributes.addFlashAttribute("message", "博客更新失败");
        } else {
            redirectAttributes.addFlashAttribute("message", "博客更新成功");
        }
        return "redirect:/admin/blogs";
    }

}
